<?php

namespace app\supplier\controller\product;

use app\supplier\controller\Controller;
use app\supplier\model\product\Category as CategoryModel;
use app\supplier\model\product\Product as ProductModel;
use app\supplier\model\product\Recommend as RecommendModel;
use app\supplier\model\product\RecommendList;
use app\supplier\model\supplier\Supplier;

/**
 * 推荐管理控制器
 */
class Recommend extends Controller
{
    /**
     * 获取列表
     */
    public function index()
    {
        // 列表
        $model = new RecommendModel;
        $list = $model->getList($this->postData(), $this->getSupplierId());
        return $this->renderSuccess('', compact('list'));
    }

    /**
     * 推荐商品选中id
     * @return \think\response\Json
     * @Author joey
     * @date 2024/3/21 16:35
     */
    public function selectedIds()
    {
        $param = $this->postData();
        if (!isset($param['module_id'])) {
            return $this->renderError('缺少module_id参数');
        }
        $list = (new RecommendList)->where(['module_id' => $param['module_id']])->column('product_id');
        return $this->renderSuccess('', compact('list'));
    }

    /**
     * 添加
     */
    public function add()
    {
        $shAuth = Supplier::checkauth($this->getSupplierId(),'recommend');
        $model = new RecommendModel;
        // 新增记录
        if ($model->add(array_merge($this->postData(), $shAuth?['audit_status' => 1]:['audit_status' => 0]), $this->getSupplierId())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     */
    public function edit($id)
    {
        $shAuth = Supplier::checkauth($this->getSupplierId(),'recommend');
        // 详情
        $model = RecommendModel::detail($id);
        if ($model->isEmpty() || $model['shop_supplier_id'] != $this->getSupplierId()) {
            return $this->renderError('暂无权限操作');
        }
        // 更新记录
        if ($model->edit(array_merge($this->postData(), $shAuth?['audit_status' => 1]:['audit_status' => 0]), $this->getSupplierId())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除
     */
    public function delete($id)
    {
        $model = RecommendModel::detail($id);
        if ($model->isEmpty() || $model['shop_supplier_id'] != $this->getSupplierId()) {
            return $this->renderError('暂无权限操作');
        }
        if (!$model->remove()) {
            return $this->renderError('该推荐使用中，删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 某一推荐下商品列表
     * @return \think\response\Json
     * @throws \Exception
     * @Author joey
     * @date 2024/3/21 11:13
     */
    public function list()
    {
        $param = $this->getData();

        if (!isset($param['module_id'])) {
            return $this->renderError('module_id不能为空');
        }

        $list = (new RecommendList)->getList($param, $this->getSupplierId());
        // 商品分类
        $category = (new CategoryModel)->getCacheTree();
        return $this->renderSuccess('', compact('list', 'category'));
    }

    /**
     *
     * getRecommendProductlistData
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/5/16 17:32
     */
    public function getRecommendProductlistData()
    {
        $param = $this->getData();
        if (!isset($param['module_id'])) {
            return $this->renderError('module_id不能为空');
        }
        $model = new ProductModel;
        $list = $model->getProductList($this->getSupplierId(), $this->postData());
        $recommend_list = (new RecommendList)->RecommendList($param, $this->getSupplierId());

        foreach ($list as $key => $value){
            $list[$key]['isChoose'] = false;
            foreach ($recommend_list as $k => $v){
                if($value['product_id'] == $v['product_id']){
                    $list[$key]['isChoose'] = true;
                }

            }
            $list[$key] = $value;

        }
        // 商品分类
        $category = (new CategoryModel)->getCacheTree();
        return $this->renderSuccess('', compact('list', 'category'));
    }


    /**
     * 添加推荐商品
     * @return \think\response\Json
     * @throws \Exception
     * @Author joey
     * @date 2024/3/21 11:13
     */
    public function addList()
    {
        $param = $this->postData();

        $obj = new RecommendList();

        if (!isset($param['module_id'])) {
            return $this->renderError('module_id不能为空');
        }
        $model = RecommendModel::detail($param['module_id']);
        if ($model->isEmpty() || $model['shop_supplier_id'] != $this->getSupplierId()) {
            return $this->renderError('暂无权限操作');
        }
        if (isset($param['ids']) && $param['ids']) {
            $param['ids'] = is_array($param['ids']) ? $param['ids'] : [$param['ids']];
            // 查询新增
            $record = $obj->where(['module_id' => $param['module_id']])->whereIn('product_id', $param['ids'])->count();
            if ($record) {
                return $this->renderError('该商品已添加');
            }
            $systime = time();
            $data    = [];
            foreach ($param['ids'] as $id) {
                $data[] = [
                    'module_id'   => $param['module_id'],
                    'product_id'  => $id,
                    'create_time' => $systime,
                    'update_time' => $systime,
                    'sort' => 100
                ];
            }
            $obj->addAll($data);
        }
        return $this->renderSuccess('保存成功');
    }

    /**
     * 编辑推荐商品
     * @return \think\response\Json
     * @throws \Exception
     * @Author joey
     * @date 2024/3/21 11:13
     */
    public function editList($id)
    {
        $param  = array_filter($this->postData(), function ($item) {
            return $item !== '';
        });
        $model = RecommendList::detail($id);
        if ($model->isEmpty()) {
            return $this->renderError('数据不存在');
        }
        $model1 = RecommendModel::detail($model['module_id']);
        if ($model1->isEmpty() || $model1['shop_supplier_id'] != $this->getSupplierId()) {
            return $this->renderError('暂无权限操作');
        }
        $res = $model->allowField(['status', 'sort'])->save($param);
        return $this->renderSuccess($res ? '保存成功' : '保存失败');
    }

    /**
     * 删除推荐商品
     * @return \think\response\Json
     * @throws \Exception
     * @Author joey
     * @date 2024/3/21 11:13
     */
    public function delList()
    {
        $param  = $this->postData();
        $model  = RecommendList::where($param)->find();
        $model1 = RecommendModel::detail($param['module_id']);
        if ($model1->isEmpty() || $model1['shop_supplier_id'] != $this->getSupplierId()) {
            return $this->renderError('暂无权限操作');
        }
        if ($model->isEmpty() || !$model->delete()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}